﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Collections
Imports System.Drawing
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Serialization
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.Runtime
Imports System.Workflow.Activities
Imports System.Workflow.Activities.Rules
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Workflow
Imports Microsoft.SharePoint.WorkflowActions
Imports System.Windows
Imports Microsoft.Office.Server
Imports Microsoft.Office.Server.UserProfiles

Public Class LookupUserProfile
    Inherits Activity

#Region " Dependancy Properties "

    <Description("Context"), ValidationOption(ValidationOption.Required), Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    Public Property __Context() As WorkflowContext
        Get
            Return GetValue(__ContextProperty)
        End Get

        Set(ByVal value As WorkflowContext)
            SetValue(__ContextProperty, value)
        End Set
    End Property

    Public Shared ReadOnly __ContextProperty As System.Workflow.ComponentModel.DependencyProperty = _
                           System.Workflow.ComponentModel.DependencyProperty.Register("__Context", _
                           GetType(WorkflowContext), GetType(LookupUserProfile))


    <Description("UserName"), ValidationOption(ValidationOption.Required), Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    Public Property UserName() As String
        Get
            Return GetValue(UserNameProperty)
        End Get

        Set(ByVal value As String)
            SetValue(UserNameProperty, value)
        End Set
    End Property

    Public Shared ReadOnly UserNameProperty As System.Workflow.ComponentModel.DependencyProperty = _
                           System.Workflow.ComponentModel.DependencyProperty.Register("UserName", _
                           GetType(String), GetType(LookupUserProfile))



    <Description("ProfileProperty"), ValidationOption(ValidationOption.Required), Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    Public Property ProfileProperty() As String
        Get
            Return GetValue(ProfilePropertyProperty)
        End Get

        Set(ByVal value As String)
            SetValue(ProfilePropertyProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ProfilePropertyProperty As System.Workflow.ComponentModel.DependencyProperty = _
                           System.Workflow.ComponentModel.DependencyProperty.Register("ProfileProperty", _
                           GetType(String), GetType(LookupUserProfile))



    <Description("PropertyValueVariable"), ValidationOption(ValidationOption.Required), Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    Public Property PropertyValueVariable() As String
        Get
            Return GetValue(PropertyValueVariableProperty)
        End Get

        Set(ByVal value As String)
            SetValue(PropertyValueVariableProperty, value)
        End Set
    End Property

    Public Shared ReadOnly PropertyValueVariableProperty As System.Workflow.ComponentModel.DependencyProperty = _
                           System.Workflow.ComponentModel.DependencyProperty.Register("PropertyValueVariable", _
                           GetType(String), GetType(LookupUserProfile))


#End Region

    Protected Overrides Function Execute(ByVal executionContext As System.Workflow.ComponentModel.ActivityExecutionContext) As System.Workflow.ComponentModel.ActivityExecutionStatus
        Return MyBase.Execute(executionContext)
        Try
            Dim err As String = String.Empty

            Dim context As ServerContext = ServerContext.GetContext(Me.__Context.Site)
            Dim profileManager As New UserProfileManager(context)
            Dim oProfile As UserProfile = profileManager.GetUserProfile(UserName)

            If oProfile Is Nothing Then
                err = "Could not load users profile"
            Else

                Dim vals As UserProfileValueCollection = oProfile(ProfileProperty)
                If vals Is Nothing Then
                    err = "Could not load property"
                Else
                    PropertyValueVariable = vals.Value
                End If

            End If
            If Not String.IsNullOrEmpty(err) Then
                LogExceptionToWorkflowHistory(New ArgumentOutOfRangeException(err), executionContext, Me.WorkflowInstanceId)
            End If

        Catch ex As Exception
            LogExceptionToWorkflowHistory(ex, executionContext, Me.WorkflowInstanceId)
        End Try


        Return ActivityExecutionStatus.Closed
        '-----------------------------------------------                      
    End Function

    ''' <summary>
    ''' logs exceptions to sharepoint's workflow history log
    ''' </summary>
    Private Sub LogExceptionToWorkflowHistory(ByVal e As Exception, ByVal context As ActivityExecutionContext, ByVal wrkflowInstanceID As Guid)
        Dim service As ISharePointService = context.GetService(GetType(ISharePointService))

        If service Is Nothing Then
            Throw New InvalidOperationException()
        End If

        Dim x As New System.Text.StringBuilder

        x.AppendLine("UserName: " & UserName)
        x.AppendLine("ProfileProperty: " & ProfileProperty)
        x.AppendLine("PropertyValueVariable: " & PropertyValueVariable)

        service.LogToHistoryList(wrkflowInstanceID, SPWorkflowHistoryEventType.WorkflowError, 0, TimeSpan.MinValue, "Error", e.ToString(), x.ToString)
    End Sub

    ''' <summary>
    ''' logs exceptions to sharepoint's workflow history log
    ''' </summary>
    Private Sub LogMessageToWorkflowHistory(ByVal m As String, ByVal context As ActivityExecutionContext, ByVal wrkflowInstanceID As Guid)
        Dim service As ISharePointService = context.GetService(GetType(ISharePointService))

        If service Is Nothing Then
            Throw New InvalidOperationException()
        End If

        Dim x As New System.Text.StringBuilder

        x.AppendLine("UserName: " & UserName)
        x.AppendLine("ProfileProperty: " & ProfileProperty)
        x.AppendLine("PropertyValueVariable: " & PropertyValueVariable)

        service.LogToHistoryList(wrkflowInstanceID, SPWorkflowHistoryEventType.WorkflowComment, 0, TimeSpan.MinValue, "Message", m, x.ToString)
    End Sub




End Class
